package com.ol.edu.client.controller.user;


import com.ol.edu.client.entity.ResponseDTO;
import com.ol.edu.client.entity.User;
import com.ol.edu.client.entity.UserDTO;
import com.ol.edu.client.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @BelongsProject: lagou-edu-web
 * @Author: GuoAn.Sun
 * @CreateTime: 2020-09-08 11:22
 * @Description:
 */

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户登陆
     * @param phone
     * @param password
     * @param nickname
     * @param headimg
     * @return
     */
    @GetMapping("/login")
    public UserDTO login(String phone, String password, String nickname, String headimg) {
        UserDTO dto = new UserDTO();
        User user = null;
        System.out.println("phone = " + phone);
        System.out.println("password = " + password);
        System.out.println("nickname = " + nickname);

        // 检测手机号是否注册
        Integer i = userService.checkPhone(phone);
        if(i == 0){
            // 未注册，自动注册并登录
            userService.register(phone, password,nickname,headimg);
            dto.setMessage("手机号尚未注册，系统已帮您自动注册，请牢记密码！");
            user = userService.login(phone, password);
        }else{
            user = userService.login(phone, password);
            if(user == null){
                dto.setState(300); //300表示失败
                dto.setMessage("帐号密码不匹配，登录失败！");
            }else{
                dto.setState(200); //200表示成功
                dto.setMessage("登录成功！");
            }
        }

        dto.setContent(user);
        return dto;
    }

    /**
     * 更新用户头像和昵称
     * @param user
     * @return
     */
    @PostMapping("/updateUserInfo")
    public ResponseDTO updateUserInfo(@RequestBody User user) {
        ResponseDTO responseDTO = userService.updateUserInfo(user);
        return responseDTO;
    }

    /**
     * 更新用户密码
     * @param user
     * @return
     */
    @PostMapping("/updatePassword")
    public ResponseDTO updatePassword(@RequestBody User user) {
        ResponseDTO responseDTO = userService.updatePassword(user);
        return responseDTO;
    }
}
